package com.lagou.dao;

import com.lagou.entity.Student;
import com.lagou.util.DruidUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao{
    //获取druid连接池的连接
    Connection c = DruidUtils.getConnection();
    PreparedStatement ps = null;
    ResultSet rs = null;
    @Override
    public int add(Student student) {
        String sql = "insert into student (id,username,password,sex,birthday,age,cid,cname) values(?,?,?,?,?,?,?,?)";
        try {
            ps = c.prepareStatement(sql);
            ps.setString(1,null);
            ps.setString(2,student.getUsername());
            ps.setString(3,student.getPassword());
            ps.setString(4,student.getSex());
            ps.setString(5,student.getBirthday());
            ps.setInt(6,student.getAge());
            ps.setInt(7,student.getCid());
            ps.setString(8,student.getCname());
            System.out.println(student);
            int i = ps.executeUpdate();
            if(1==i){
                return 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtils.close(c,ps);
        }
        return 0;
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from student where id = ?";
        try {
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if(i==1){
                // 成功为1
                return 1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtils.close(c,ps);

        }
        // 失败为0
        return 0;
    }



    @Override
    public List<Student> list() {
        ArrayList<Student> students = new ArrayList<>();
        String sql = "select * from student";
        try {
            ps = c.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Student stu = new Student(
                        rs.getString("username"),
                        rs.getString("password"),
                        rs.getString("sex"),
                        rs.getString("birthday"),
                        rs.getInt("age"),
                        rs.getString("cname"));
                students.add(stu);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtils.close(c,ps,rs);
        }
        return students;
    }
 @Test
    public void test(){
        StudentDao dao = new StudentDaoImpl();
        Student student = new Student("oyz","123456");
        Student login = dao.login(student);
        System.out.println(login);
 }

    @Override
    public Student login(Student student) {
        try {

            String sql = "select * from student where username = ? and password = ?";
            ps = c.prepareStatement(sql);
            ps.setString(1,student.getUsername());
            ps.setString(2,student.getPassword());
            rs = ps.executeQuery();
            if(rs.next()){
                String username = rs.getString("username");
                String password = rs.getString("password");
                System.out.println("姓名:"+username+"密码:"+password);
                Student student1 = new Student(username, password);
                return student1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtils.close(c,ps,rs);
        }
        return null;
    }
}
